home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / beans / simplebean / example / TestAcme05Bean.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.5 KB  |  97 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import acme.beans.Acme05Bean;
  18. import java.awt.*;
  19. import java.applet.*;
  20.  
  21. public class TestAcme05Bean extends Applet
  22. {
  23.    String msgStr;
  24.    private Acme05Bean b;
  25.  
  26.    public void init()
  27.    {
  28.      b = new Acme05Bean();
  29.      add(b);
  30.       setFont(new Font("TimesRoman", Font.BOLD, 24));
  31.       msgStr = "Press any key...";
  32.       setForeground(Color.blue);
  33.       add(new Button("make action event"));
  34.       setSize(400,200);
  35.    }
  36.  
  37.    public void start()
  38.    {
  39.       requestFocus();
  40.    }
  41.  
  42.    public boolean keyDown(Event e,  int key)
  43.    {
  44.       msgStr = "\"" + (char)key + "\"" + " key pressed";
  45.       if ((char)key == 'q') {
  46.         System.exit(0);
  47.         System.out.println("Got a 'q'");
  48.       }
  49.       repaint();
  50.       return true;
  51.    }
  52.  
  53.    public boolean keyUp(Event e,  int key)
  54.    {
  55.       msgStr = "\"" + (char)key + "\"" + " key released";
  56.       repaint();
  57.       return true;
  58.    }
  59.  
  60.    public boolean gotFocus(Event e,  Object arg)
  61.    {
  62.       msgStr = "Got Focus";
  63.       if (arg!= null)
  64.          msgStr = msgStr + " arg: " + arg.toString();
  65.       repaint();
  66.       return true;
  67.    }
  68.  
  69.    public boolean lostFocus(Event e,  Object arg)
  70.    {
  71.       msgStr = "Lost Focus" ;
  72.       if (arg != null)
  73.          msgStr = msgStr + " arg: " + arg.toString();
  74.       repaint();
  75.       return true;
  76.    }
  77.  
  78.    public boolean action(Event e,  Object arg)
  79.    {
  80.       msgStr = "Action Event." ;
  81.       if (arg != null)
  82.          msgStr = msgStr + " Arg: " + arg.toString();
  83.       repaint();
  84.       return true;
  85.    }
  86.  
  87.    public void paint(Graphics g)
  88.    {
  89.       FontMetrics currFM = g.getFontMetrics();
  90.       g.drawString(msgStr, ( getSize().width -
  91.       currFM.stringWidth(msgStr)) / 2,
  92.       (getSize().height- currFM.getHeight())/ 2 +
  93.       currFM.getLeading() + currFM.getAscent());
  94.     }
  95.  
  96. }
  97.